package net.rong.mobileim;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import net.x52im.mobileimsdk.android.event.ChatMessageEvent;

public class ChatMessageEventListener implements ChatMessageEvent {

    private final ReactApplicationContext reactContext;

    public ChatMessageEventListener(ReactApplicationContext reactContext) {
        this.reactContext = reactContext;
    }

    @Override
    public void onRecieveMessage(String fp,
                                 String fromId,
                                 String dataContent,
                                 int typeU) {
        WritableMap params = Arguments.createMap();
        params.putString("fp", fp);
        params.putString("fromId", fromId);
        params.putString("dataContent", dataContent);
        params.putInt("typeU", typeU);

        reactContext
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit(EventConst.ON_RECEIVE_MESSAGE, params);
    }

    @Override
    public void onErrorResponse(int code,
                                String errMsg) {
        WritableMap params = Arguments.createMap();
        params.putInt("code", code);
        params.putString("msg", errMsg);

        reactContext
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit(EventConst.ON_ERROR_RESPONSE, params);
    }

}
